﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using System.Text;

namespace My3DGame
{
    public class clsIntroState : clsStateGame
    {
        double timeDisplay;
        List<Texture2D> backgroundImage;
        Vector2 posLogo = new Vector2();
        Vector2 posLineL = new Vector2();
        Vector2 posLineR = new Vector2();

        public clsIntroState(Game1 game1)
        {
            timeDisplay = 0.0;
            nextGameState = clsStateGameManager.StateGame.MENU;
        }

        public override void Initialize(Game1 game1)
        {
            clsResourceHelper.LoadIntroImages();
            backgroundImage = clsResourceHelper.IntroImages;
            // Xác định vị trí của logo.
            posLogo.X = game1.Window.ClientBounds.Width / 2 - backgroundImage[0].Width / 2;
            posLogo.Y = game1.Window.ClientBounds.Height / 2 - backgroundImage[0].Height / 2 - 50;

            // Xác định vị trí ban đầu của LineL
            posLineL.X = game1.Window.ClientBounds.Width / 2 - backgroundImage[2].Width;
            posLineL.Y = posLineR.Y = game1.Window.ClientBounds.Height / 2 - backgroundImage[0].Height / 2 - 50;
            // Xác định vị trí ban đầu của LineR.
            posLineR.X = game1.Window.ClientBounds.Width / 2;// -backgroundImage[1].Width;
        }

        public override void Update(GameTime gameTime, Game1 game1)
        {
            #region Chạy logo từ góc trái trên xuống chính giữa màn hình
            //if (posLogo.X < FOA_Main.Window.ClientBounds.Width / 2 - brackgroundImage[0].Width / 2 &&
            //    posLogo.Y < FOA_Main.Window.ClientBounds.Height / 2 - brackgroundImage[0].Height / 2)
            //{
            //    float per = FOA_Main.Window.ClientBounds.Width / FOA_Main.Window.ClientBounds.Height;
            //    posLogo.X += per*FOA_Main.Window.ClientBounds.Width/200;
            //    posLogo.Y += per*FOA_Main.Window.ClientBounds.Height/200;
            //}
            //else
            //{
            //    // Dừng 2 sec trước khi qua menu.
            //    timeDisplay += gameTime.ElapsedGameTime.TotalSeconds;
            //    if (timeDisplay > 2.0)
            //    {
            //        FOA_Main.gameStateManager.NextState(FOA_Main);
            //    }
            //}
            #endregion

            #region Làm intro giống Need for Speed.
            int space = 10;
            if (posLineL.X > game1.Window.ClientBounds.Width / 2 - backgroundImage[0].Width / 2 - backgroundImage[2].Width - space &&
                posLineR.X < game1.Window.ClientBounds.Width / 2 + backgroundImage[0].Width / 2 + backgroundImage[3].Width + space)
            {
                posLineL.X -= 1;
                posLineR.X += 1;
            }
            else
            {
                // Dừng 2 sec trước khi qua menu.
                timeDisplay += gameTime.ElapsedGameTime.TotalSeconds;
                if (timeDisplay > 2.0)
                {
                    game1.stateGameManager.NextState(game1);
                }
            }

            #endregion
        }

        public override void Draw(GameTime gameTime, Game1 game1)
        {
            game1.spriteBatch.Begin();
            // Vẽ logo giữa màn hình.
            game1.spriteBatch.Draw(backgroundImage[0], posLogo, Color.White);

            // Vẽ 2 line.
            game1.spriteBatch.Draw(backgroundImage[2], posLineL, Color.White);
            game1.spriteBatch.Draw(backgroundImage[3], posLineR, Color.White);

            game1.spriteBatch.End();
        }
    }
}
